package com.tsd.system.service.impl;

import com.tsd.core.constants.JwtConstants;
import com.tsd.core.utils.HlpUtils;
import com.tsd.core.utils.ListUtil;
import com.tsd.core.vo.PageBean;
import com.tsd.jwt.JwtHelper;
import com.tsd.jwt.JwtRefreshUtil;
import com.tsd.service.BaseServiceImpl;
import com.tsd.system.dao.SysRegUserBindMapper;
import com.tsd.system.dao.SysUserMapper;
import com.tsd.system.entity.SysRegUserBind;
import com.tsd.system.entity.SysRegUserBindExt;
import com.tsd.system.entity.SysUser;
import com.tsd.system.entity.SysUserExt;
import com.tsd.system.service.SysRegUserBindService;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;

/**
 * 描述：SysRegUserBind 服务实现层
 *
 * @author Hillpool
 * @date 2022/06/30 14:05:24
 */
@Service
public class SysRegUserBindServiceImpl extends BaseServiceImpl implements SysRegUserBindService {

    @Resource
    private SysRegUserBindMapper sysRegUserBindMapper;
    @Resource
    private SysUserMapper sysUserMapper;

    @Override
    public SysRegUserBind getSysRegUserBindById(Long id) throws Exception {
        return sysRegUserBindMapper.selectByPrimaryKey(id);
    }

    @Override
    public SysRegUserBindExt queryBySid(String sid) throws Exception {
        return sysRegUserBindMapper.selectBySid(sid);
    }

    @Override
    public void deleteSysRegUserBinds(List<String> list, SysUser opUser) throws Exception {
        for (String sid : list) {
            SysRegUserBind old = sysRegUserBindMapper.selectBySid(sid);
            super.checkEmpty(old, "该记录不存在");
            super.autoInjectBaseData(old, opUser, TYPE_DELETE);
            sysRegUserBindMapper.updateByPrimaryKeySelective(old);
        }
    }

    @Override
    public void saveSysRegUserBind(SysRegUserBind record, SysUser opUser) throws Exception {
        if (HlpUtils.isEmpty(record.getId())) {
            super.autoInjectBaseData(record, opUser, this.TYPE_CREATE);
            sysRegUserBindMapper.insertSelective(record);
        } else {
            super.autoInjectBaseData(record, opUser, this.TYPE_UPDATE);
            sysRegUserBindMapper.updateByPrimaryKeySelective(record);
        }
    }

    @Override
    public List<SysRegUserBindExt> findSysRegUserBinds(Map<String, Object> params, PageBean page) throws Exception {
        return sysRegUserBindMapper.selectByPage(params, page);
    }

    @Override
    public List<SysRegUserBindExt> findSysRegUserBinds(Map<String, Object> params) throws Exception {
        return sysRegUserBindMapper.selectByParams(params);
    }

    @Override
    public List<SysUserExt> queryValid4Bind(SysUserExt opUser, String targetType, boolean withToken) throws Exception {
        List<SysUserExt> resultList = new ArrayList<>();
        SysRegUserBindExt params = new SysRegUserBindExt();
        params.setDr(0);
        params.setReg_user_sid(opUser.getSid());
        if (!HlpUtils.isEmpty(targetType)) {
            params.setTarget_type(targetType);
        }
        List<SysRegUserBindExt> bindExtList = sysRegUserBindMapper.selectByEntity(params);
        if (!HlpUtils.isEmptyList(bindExtList)) {
            List<SysRegUserBindExt> bindUserList = ListUtil.filter(bindExtList, t -> SysRegUserBindExt.TARGET_TYPE_USER.equals(t.getTarget_type()));
            if (!HlpUtils.isEmptyList(bindUserList)) {
                List<String> sidList = ListUtil.map(bindUserList, SysRegUserBindExt::getTarget_sid);
                List<SysUserExt> userExtList = sysUserMapper.selectVaildBySidList(sidList);
                if (!HlpUtils.isEmptyList(userExtList)) {
                    for (SysUserExt userExt : userExtList) {
                        SysUserExt ext = userExt.cloneBase();
                        ext.setOpen_id(opUser.getOpen_id());
                        ext.setToken_type(JwtConstants.TYPE_SYS_USER);
                        if (withToken) {
                            JwtRefreshUtil.createSysUserToken(JwtConstants.TYPE_SYS_USER, ext);
                        }
                        resultList.add(ext);
                    }
                }
            }
        }
        return resultList;
    }
}
